
com.sxnsx.xnaja.downloader.douban = function () {
var pub = {};
var priv = {};
var tag = "com.sxnsx.xnaja.downloader.douban";

/*
 * inherit from abs_downloader
for (var e in com.sxnsx.xnaja.downloader.abs_downloader) {
    pub[e] = com.sxnsx.xnaja.downloader.abs_downloader[e]
}
* */

this.tag = tag;
/*
 *  downloader specified methods
 * */
this.do_proc = function (url, proc) {
	var pages = this.emnu_page_url(url);
    //alert(pages);
	for (var each in pages){
		var photos = this.emnu_photo_url(pages[each]);
		//alert(photos);
        for (var photo_url in photos) {
			if (!this.working)
				return;
			status_info = "Pages:" + (parseInt(each) + 1) 
				+ "/" + pages.length.toString() 
				+ "; Photo:" + (parseInt(photo_url) + 1)
				+ "/" + photos.length.toString();
            xnaja_app.ui_overlay.updata_status('download', status_info);
            link = xnaja_app.utilities.xnaja_go(photos[photo_url]).match(/<a class="mainphoto" .*?\s*?<img src="(.*?)"/)[1];
            proc(link);
		}
	}
}


this.emnu_page_url = function (url) {
    var ret = [];
    var vals = [];
    var flag = '<span class="count">';
    var reg = RegExp(flag);
    var src = xnaja_app.utilities.xnaja_go(url);
    var offset= src.search(reg);
    
    //alert(src.indexOf(flag));
    if (offset == -1) {
        alert('NO')
        return null;
    }
    offset += flag.length;
    //由于go函数得到的源码非ASCII码是乱码
    //所以只有暴力搜索相册中照片的数量
    //第1-25张 对应 231,172,172, 49, 45, 50,53, 229,188,160
    //共131张  对应 229,133,177, 49,51,49, 229,188,160
    for (var i = 0; i < 100; ++i) {
        //获取照片总数
        if (src.charCodeAt(offset + i) == 229
            && src.charCodeAt(offset + i + 1) == 133)
        {
            var t = ''
            var j = i + 3;
            while (src.charCodeAt(offset + j) <= 57
                    && src.charCodeAt(offset + j) >= 48) {
                t += src[offset + j];
                j+=1;
            }
            this.total_photo = parseInt(t);
            break;
        }
    }
    if (this.total_photo <= 18){
        this.total_page = 1;
    } else {
        this.total_page = parseInt(this.total_photo / 18);
        if (this.total_photo % 18 != 0)
            this.total_page += 1;
    }

    //alert(this.total_page+' pages,'+this.total_photo+' photos');
    var base_url = url.substr(0, url.lastIndexOf('/')+1);

    var begin_page = this.page_from == -1? 0: this.page_from - 1;
    var end_page = this.page_to == -1? this.total_page:
                    this.page_to > this.total_page? this.total_page:
                        this.page_to;

    for (var i = begin_page; i < end_page ; i = i + 1) {
        ret.push( base_url 
            + '?start=' + i * 18);
    }
    //alert(ret.toSource());
    return ret;
};


this.emnu_photo_url = function (url) {
	var ret = [];
	var trs = xnaja_app.utilities.xnaja_go(url).match(/<a href=".*?" class="photolst_photo"/g);
	for (var each in trs) {
		ret.push('http://www.douban.com/'+ trs[each].match(/<a href="(.*)" /)[1]);
	}
	return ret;
};

};

com.sxnsx.cexport(com.sxnsx.xnaja.downloader.douban, com.sxnsx.xnaja.downloader.abs_downloader);

